package com.hanhai.zrb.api.mongodb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import com.hanhai.zrb.model.project.ProjectDetail;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;

public class ProjectDetailMongodbClient {

	public static final String CON_NAME = "projectDetail";

	private Logger log = Logger.getLogger(getClass());

	public void add(ProjectDetail projectDetail) {
		DBCollection con = getCon();
		add(con, projectDetail);
	}

	private DBCollection getCon() {
		DB db = MongoUtil.db();
		DBCollection con = db.getCollection(CON_NAME);
		return con;
	}

	// 增加
	private DBCollection add(DBCollection projectDetailCollection,
			ProjectDetail projectDetail) {
		DBObject object = BeanUtil.bean2DBObject(projectDetail);
		WriteResult wr = projectDetailCollection.insert(object);
		CommandResult cr = wr.getLastError();
		log.info("Add new projectDetail,result:" + cr);
		return projectDetailCollection;
	}

	public void update(ProjectDetail projectDetail) {
		DBCollection con = getCon();
		update(con, projectDetail);
	}

	// 修改
	private void update(DBCollection collection, ProjectDetail projectDetail) {
		if (projectDetail.getId() == null) {
			log.error("Update projectDetail,must have a unique id");
			return;
		}

		BasicDBObject updateCondition = new BasicDBObject();
		updateCondition.append("id", projectDetail.getId());

		DBObject newObject = BeanUtil.bean2DBObject(projectDetail);

		DBObject updateSetValue = new BasicDBObject("$set", newObject);
		WriteResult wr = collection.update(updateCondition, updateSetValue);
		log.info("Update new projectDetail,result:" + wr);
	}

	public ProjectDetail get(long id) {
		DBCollection con = getCon();
		ProjectDetail projectDetail = findById(con, id);
		return projectDetail;
	}

	// 从集合中，根据ID查找
	private ProjectDetail findById(DBCollection collection, Long id) {
		BasicDBObject searchProjectDetailById = new BasicDBObject();
		searchProjectDetailById.append("id", id);
		ProjectDetail projectDetailBefore = null;
		// findOne方法更简单一些
		DBCursor cursor = collection.find(searchProjectDetailById);
		while (cursor.hasNext()) {
			DBObject articleObject = cursor.next();
			if (articleObject != null) {
				projectDetailBefore = objectToArticle(articleObject);
				String internalId = articleObject.get("_id").toString();
				projectDetailBefore.setMongoId(internalId);
			}
		}
		cursor.close();
		return projectDetailBefore;
	}

	// 对象转换
	private ProjectDetail objectToArticle(DBObject object) {
		ProjectDetail projectDetail = new ProjectDetail();
		// 用工具方法转换，手动转换，需要判断类型，比较麻烦
		projectDetail = BeanUtil.dbObject2Bean(object, projectDetail);
		return projectDetail;
	}

	public List<ProjectDetail> getProjectInfoBasic(List<Long> idList) {
		DBCollection con = getCon();
		List<ProjectDetail> list = findByIdList(con, idList);
		return list;
	}

	// 根据ID集合查找
	private List<ProjectDetail> findByIdList(DBCollection collection,
			List<Long> idList) {
		BasicDBList values = new BasicDBList();
		values.addAll(idList);

		DBObject inQuery = new BasicDBObject("$in", values);

		DBObject con = new BasicDBObject();
		con.put("id", inQuery);
		DBCursor cursorIdArray = collection.find(con);

		List<ProjectDetail> projectDetailList = new ArrayList<ProjectDetail>();
		while (cursorIdArray.hasNext()) {
			DBObject articleObject = cursorIdArray.next();
			ProjectDetail projectDetail = new ProjectDetail();
			BeanUtil.dbObject2Bean(articleObject, projectDetail);
			String mongoId = articleObject.get("_id").toString();
			projectDetail.setMongoId(mongoId);

			projectDetailList.add(projectDetail);
		}
		return projectDetailList;
	}

	public static Map<Long,ProjectDetail> convertListToMap(List<ProjectDetail> list){
		Map<Long,ProjectDetail> map = new HashMap<Long, ProjectDetail>();
		if(CollectionUtils.isNotEmpty(list)){
			for(ProjectDetail detail:list){
				map.put(detail.getId(), detail);
			}
		}
		return map;
	}

}
